package com.example.springboot3demo.utils;

import com.example.springboot3demo.exception.BizException;

import java.util.function.Supplier;

/**
 * @author chenyuwen
 * @date 2021/11/5
 */
public class AssertUtil {

    public static void notNull(Object object, String message) {
        isTrue(object != null, () -> new BizException(message));
    }

    public static void isNull(Object object, String message) {
        isTrue(object == null, () -> new BizException(message));
    }

    public static void notZero(Number number, String message) {
        isTrue(!number.equals(0), () -> new BizException(message));
    }

    public static void isZero(Number number, String message) {
        isTrue(number.equals(0), () -> new BizException(message));
    }

    public static void isFalse(boolean expression, String message) {
        isTrue(!expression, message);
    }

    public static void isTrue(boolean expression, String message) {
        isTrue(expression, () -> new BizException(message));
    }

    public static <X extends Throwable> void isTrue(boolean expression, Supplier<? extends X> supplier) throws X {
        if (expression) {
            return;
        }
        throw supplier.get();
    }

    public static <V> V isException(Supplier<? extends V> supplier, String message) {
        try {
            return supplier.get();
        } catch (Exception e) {
            throw new BizException(message);
        }
    }

    public static void throwException(String message) {
        throw new BizException(message);
    }
}
